//严格模式
'use strict';

var gulp = require('gulp');
var sass = require('gulp-sass');
var autoprefixer = require('gulp-autoprefixer');
var csso = require('gulp-csso');
var browserSync = require('browser-sync').create();
var fileinclude = require('gulp-file-include');
var header = require('gulp-header');
var pkg = require('./package.json');
var gulpLoadPlugins = require('gulp-load-plugins');
var size = require('gulp-size');
var uglify = require('gulp-uglify');
var rev = require('gulp-rev');
var revcss = require('gulp-rev-css-url');
var revdel = require('gulp-rev-delete-original');
var jshint = require('gulp-jshint');
var time = require('time-require');
var clean = require('gulp-clean');
var webpack = require('gulp-webpack');
var runSequence = require('run-sequence');
var $ = gulpLoadPlugins();
var banner = [
    '/*!',
    ' * zyg-ui ${pkg.title} v${pkg.version}',
    ' * Copyright <%= new Date().getFullYear() %> zyg, Inc.,250655604@qq.com',
    ' * Licensed under the <%= pkg.license %> license',
    ' */',
    ''].join('\n');

var addBanner = function () {
    return $.header(banner, { pkg : pkg });
};

//node-sass 编译
gulp.task('sass', function () {
    return gulp.src('./sass/**/*.scss')
        .pipe(sass().on('error', sass.logError))
        .pipe(autoprefixer({
            browsers: [
                'Android >= 4',
                'iOS >= 9'
            ],
            cascade: false
        }))
        .pipe(csso())
        .pipe(size())
        .pipe(addBanner())
        .pipe(gulp.dest('./css'));
});

//watch 监控
gulp.task('watch', function () {
    gulp.watch('./sass/**/*.scss', ['sass']);
    gulp.watch('./images/**/*.**', ['watch']);
    gulp.watch('./js/**/*.js', ['jshint']);
    gulp.watch('./gulpfile.js', ['jshint']);
    gulp.watch('./include/**/*.html', ['fileinclude']);
});

//localhost
gulp.task('phone', ['watch'], function () {
    browserSync.init({
        baseDir: './',
        server: './',
        port: 8080,
        files: ['css/**/*.css', 'sprite/**/*.html', 'js/**/*.js', 'images/**/*.*'],
        ui: {
            port: 8081,
            weinre: {
                port: 9090
            }
        }
    });
});

//include模板
gulp.task('fileinclude', function () {
    return gulp.src('./include/**/*.html')
        .pipe(fileinclude({
            prefix: '@@',
            basepath: '@file'
        }))
        .pipe(size())
        .pipe(gulp.dest('./'));
});

//js压缩
gulp.task('uglify', function () {
    return gulp.src('./js/**/*.js')
        .pipe(uglify())
        .pipe(size())
        .pipe(addBanner())
        .pipe(gulp.dest('./js'));
});

//MD5
gulp.task('rev', function () {
    return gulp.src(['./images/**/*.**'])
        .pipe(rev())
        .pipe(revcss())
        .pipe(revdel())
        .pipe(size())
        .pipe(gulp.dest('./images'));

});

//js校验
gulp.task('jshint', function () {
    return gulp.src('./jshintrc')
        .pipe(jshint());
});

//删除
gulp.task('clean', function () {
    return gulp.src('./sprite/src')
        .pipe(clean());
});

//webpack


//多task
gulp.task('build', function (callback) {
    runSequence('clean','rev',callback);
});